package io.yizoo.system.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.elm.core.CoreController;
import io.elm.core.HMapper;
import io.yizoo.system.entity.sys_menu;
import io.yizoo.system.entity.sys_org;
import io.yizoo.system.entity.sys_role_rel_menu;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/system/menu")
public class menuController extends CoreController {

    @Override
    protected void Init() {
        PowerEntity = sys_menu.class;
        PowerUse = EnumSet.of(Power.IDDelete, Power.IDAny, Power.IDGetSingle);
    }

    @PostMapping("/GetTree")
    public Object GetTree(@RequestBody String jsonStr) throws Exception {
        Map<String, Object> factor = Ctor.DMaker(jsonStr).Selector
                .From(sys_menu.class)
                .IsWhere("and name like CONCAT('%', #{name}, '%')","name")
                .IsPage()
                .GetFactor();
        HMapper.PageData data = Ctor.HMapper.GetTree(factor);
        return JsonResult(data);
    }

    @PostMapping("/GetPath")
    public Object GetPath(@RequestBody String jsonStr) {
        Object data = Ctor.HMapper.GetPath(
                Ctor.DMaker(jsonStr).Selector
                        .From(sys_menu.class)
                        .Select("id,pid,name")
                        .IsWhere("and id=#{id}","id")
                        .GetFactor());
        return JsonResult(data);
    }

    @PostMapping("/Upsert")
    public Object Upsert(@RequestBody String jsonStr)
    {
        String id = Ctor.HMapper.Upsert(sys_menu.class,jsonStr);
        sys_role_rel_menu mod=new sys_role_rel_menu();
        mod.setRole_id("1");
        mod.setMenu_id(id);
        Ctor.HMapper.Upsert(mod);
        return JsonResult(1);
    }

}
